package com.example.demo.controller;

import com.example.demo.pojo.Category;
import com.example.demo.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "栏目管理系统")
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    public CategoryService categoryService;
    //1
    @GetMapping("/select")
    @ApiOperation("根据ID进行查询")
    @ApiImplicitParam(name = "id",value = "栏目编号",required = true)
    public Category select(int id){
        Category category=categoryService.select(id);
        return category;
    }
    //2
    @PostMapping("/insert")
    @ApiOperation("栏目插入")
    public String save(Category category){
        categoryService.insert(category);
        return "栏目插入成功";
    }
    //3
    @PostMapping("/delete")
    @ApiOperation("栏目删除")
    @ApiImplicitParam(name = "id",value = "栏目编号",required = true)
    public String delete(int id ){
        categoryService.delete(id );
        return "栏目删除成功";
    }
    //4
    @PostMapping("/update")
    @ApiOperation("栏目更新")
    public String update(Category category ){
        categoryService.update(category );
        return "栏目更新成功";
    }
    //5
    @PostMapping("/patchdelete")
    @ApiOperation("栏目批量删除")
    @ApiImplicitParam(name = "ids",value = "栏目编号，用英文逗号分开",required = true)
    public String patchdelete(int[] ids){
        for (int id: ids){categoryService.delete(id );}
        return "栏目删除成功";
    }
    //6
    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="size",value = "页面大小",required = true),
            @ApiImplicitParam(name = "page",value = "页码",required = true)
    })
    public PageInfo seleceByPage(int size, int page){
        PageInfo pageInfo=categoryService.findByPage(size,page);
        return pageInfo;
    }
}
